package com.studynotes.dev.tech.common.api;

import com.studynotes.dev.tech.common.enums.ErrorCodeEnum;
import com.studynotes.dev.tech.common.utils.I18nMessageUtil;
import com.studynotes.dev.tech.exception.GlobalException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Optional;
/**
 * @author Liu XiaoJun
 * @description: 返回值封装
 * @date 2025-11-07 13:37:13
 */
@Data
@Accessors(chain = true)
@Schema(name = "ApiResult", description = "返回值封装")
public class ApiResult<T> implements Serializable {

    /**
     * 返回码
     */
    @Schema(name = "code", description = "错误码")
    private long code;

    /**
     * 描述
     */
    @Schema(name = "msg", description = "消息")
    private String msg;

    /**
     * 数据
     */
    @Schema(name = "data", description = "数据")
    private T data;

    /**
     * 随机数
     */
    @Schema(name = "ts", description = "随机数")
    private long ts;

    public String getMsg() {
        return I18nMessageUtil.getMessage(msg, msg);
    }

    public ApiResult(ErrorCodeEnum errorCode) {
        errorCode = Optional.ofNullable(errorCode).orElse(ErrorCodeEnum.FAILED);
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
        this.ts = System.currentTimeMillis();
    }

    public ApiResult() {

    }

    public static <T> ApiResult<T> ok() {
        return ok(null);
    }

    public static <T> ApiResult<T> ok(T data) {
        ErrorCodeEnum aec = ErrorCodeEnum.SUCCESS;
        return restResult(data, aec);
    }

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> fail(String msg) {
        return restResult(null, ErrorCodeEnum.FAILED.getCode(), msg);
    }

    public static <T> ApiResult<T> fail(Long code, String msg) {
        return restResult(null, code, msg);
    }

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> fail(ErrorCodeEnum errorCode) {
        return restResult(null, errorCode);
    }

    public static <T> ApiResult<T> failed(String msg) {
        return thrownResult(null, ErrorCodeEnum.FAILED.getCode(), msg);
    }

    public static <T> ApiResult<T> failed(Long code, String msg) {
        return thrownResult(null, code, msg);
    }

    public static <T> ApiResult<T> failed(ErrorCodeEnum errorCode) {
        return thrownResult(null, errorCode);
    }

    public static <T> ApiResult<T> thrownResult(T data, ErrorCodeEnum errorCode) {
        return thrownResult(data, errorCode.getCode(), errorCode.getMsg());
    }

    private static <T> ApiResult<T> thrownResult(T data, long code, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setTs(System.currentTimeMillis());
        if (code == ErrorCodeEnum.SUCCESS.getCode()) {
            return apiResult;
        } else {
            throw new GlobalException(apiResult.getMsg());
        }
    }

    public static <T> ApiResult<T> restResult(T data, ErrorCodeEnum errorCode) {
        return restResult(data, errorCode.getCode(), errorCode.getMsg());
    }

    private static <T> ApiResult<T> restResult(T data, long code, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setTs(System.currentTimeMillis());
        return apiResult;
    }

    public boolean isOk() {
        return ErrorCodeEnum.SUCCESS.getCode() == code;
    }

}
